<?php
class Model_Category extends Zend_Db_Table_Abstract
{
    protected $_name = 'category';

    public function getPaginator($filters = array(), $sort = null, $page = 1, $rowCount = null)
    {
        // get select query
        $select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($sort != null)
            $select->order ( $sort );
            
        // get adapter
        $adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
        
        // get paginator
        $paginator = new Zend_Paginator ( $adapter );
        $paginator->setItemCountPerPage ( $rowCount );
        $paginator->setCurrentPageNumber ( $page );
        
        return $paginator;
    }

    public function getList($filters = array(), $sort = null, $page = null, $rowCount = null)
    { 
        $select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($page != null)
            $select->limitPage ( $page, $rowCount );
        
        if ($sort != null)
            $select->order ( $sort );
            
        return $this->fetchAll ( $select );
    }
    
    public function createCategory($cateName)
    {
        // create a new row in the table
        $row = $this->createRow ();
       
        // set the row data
        $row->cat_name = $cateName;
        
        
        // save the new row
        $row->save ();
        
        // now fetch the id of the row you just created and return it 
        $id = $row->cat_id; //$this->_db->lastInsertId();
        return $id;
    }
    
    public function updateCategory($catID, $cateName)
    {
        // find the row that matches the id
        $row = $this->find ( $catID )->current ();
        if ($row)
        {
            // set the row data
            $row->cat_name = $cateName;
	        
	        if($result) {
	        	$row->result = $result;
	        }
	        
            // save the new row
            $row->save ();
            return true;
        }
        else
        {
            throw new Zend_Exception ( "Update function failed; could not find row!" );
        }
    }

    public function deleteCategory($catID)
    {
        // find the row that matches the id
        $row = $this->find ( $catID )->current ();
        if ($row)
        {
            $row->delete ();
            return true;
        }
        else
        {
            throw new Zend_Exception ( "Delete function failed; could not find row!" );
        }
    }
    
    public function getListMenu()
    {
    	$query = $this->getAdapter()->query("SELECT base.cat_id, category.cat_name, base.dim_id, dimension.dim_value
												FROM (SELECT cat_id, dim_id FROM product GROUP BY cat_id, dim_id ORDER BY cat_id, dim_id ) AS base
												LEFT JOIN category ON category.cat_id = base.cat_id
												LEFT JOIN `dimension` ON `dimension`.dim_id = base.dim_id");
    	$records = $query->fetchAll();
    	
    	$lstMenu = array();
    	foreach ($records as $rec)
    	{
    		$lstMenu[$rec['cat_id']]['cat_id'] = $rec['cat_id']; 
    		$lstMenu[$rec['cat_id']]['cat_name'] = $rec['cat_name'];
    		$lstMenu[$rec['cat_id']]['lstDimension'][] = array('dim_id'=>$rec['dim_id'], 'dim_value'=>$rec['dim_value']);
    	}
    	return $lstMenu;
    }
}
?>
